//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "MarketModelEvolver.h"
using namespace Cephei::QL::Models::Marketmodels;
#include <gen/QL/Models/Marketmodels/CurveState.h>
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::CMarketModelEvolver::CMarketModelEvolver (boost::shared_ptr<QuantLib::MarketModelEvolver>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelEvolver = NULL;
#endif
	_ppMarketModelEvolver = &childNative;
    
}
Cephei::QL::Models::Marketmodels::CMarketModelEvolver::CMarketModelEvolver (QuantLib::MarketModelEvolver& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phMarketModelEvolver = NULL;
#endif
	_ppMarketModelEvolver = new boost::shared_ptr<QuantLib::MarketModelEvolver> (&childNative);
    
    _MarketModelEvolverOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::CMarketModelEvolver::CMarketModelEvolver (CMarketModelEvolver^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelEvolver = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMarketModelEvolver = new boost::shared_ptr<QuantLib::MarketModelEvolver> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::CMarketModelEvolver::CMarketModelEvolver (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelEvolver = NULL;
#endif
	if (!t->IsSubclassOf(CMarketModelEvolver::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::CMarketModelEvolver::CMarketModelEvolver (QuantLib::Handle<QuantLib::MarketModelEvolver>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMarketModelEvolver = &childNative;
	_ppMarketModelEvolver = &static_cast<boost::shared_ptr<QuantLib::MarketModelEvolver>>(childNative.currentLink());
    
    _MarketModelEvolverOwner = owner;
}
Cephei::QL::Models::Marketmodels::CMarketModelEvolver::CMarketModelEvolver (QuantLib::Handle<QuantLib::MarketModelEvolver> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMarketModelEvolver = &childNative;
	_ppMarketModelEvolver = &static_cast<boost::shared_ptr<QuantLib::MarketModelEvolver>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::CMarketModelEvolver::CMarketModelEvolver (QuantLib::MarketModelEvolver childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelEvolver = NULL;
#endif
	_ppMarketModelEvolver = new boost::shared_ptr<QuantLib::MarketModelEvolver> (new QuantLib::MarketModelEvolver (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::CMarketModelEvolver::~CMarketModelEvolver ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppMarketModelEvolver != NULL)
    {
	    delete _ppMarketModelEvolver;
        _ppMarketModelEvolver = NULL;
    }
}
Cephei::QL::Models::Marketmodels::CMarketModelEvolver::!CMarketModelEvolver ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppMarketModelEvolver != NULL)
    {
	    delete _ppMarketModelEvolver;
    }
}
QuantLib::MarketModelEvolver& Cephei::QL::Models::Marketmodels::CMarketModelEvolver::GetReference ()
{
    if (_ppMarketModelEvolver == NULL) throw REFNEW NativeNullException ();
	return **_ppMarketModelEvolver;
}
boost::shared_ptr<QuantLib::MarketModelEvolver>& Cephei::QL::Models::Marketmodels::CMarketModelEvolver::GetShared ()
{
    if (_ppMarketModelEvolver == NULL) throw REFNEW NativeNullException ();
	return *_ppMarketModelEvolver;
}
QuantLib::MarketModelEvolver* Cephei::QL::Models::Marketmodels::CMarketModelEvolver::GetPointer ()
{
    if (_ppMarketModelEvolver == NULL) throw REFNEW NativeNullException ();
	return &**_ppMarketModelEvolver;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MarketModelEvolver>& Cephei::QL::Models::Marketmodels::CMarketModelEvolver::GetHandle ()
{
	if (_phMarketModelEvolver == NULL)
	{
		_phMarketModelEvolver = new Handle<QuantLib::MarketModelEvolver> (*_ppMarketModelEvolver);
	}
	return *_phMarketModelEvolver;
}
#endif
bool Cephei::QL::Models::Marketmodels::CMarketModelEvolver::HasNative () 
{
	return (_ppMarketModelEvolver != NULL);
}

Double Cephei::QL::Models::Marketmodels::CMarketModelEvolver::AdvanceStep::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppMarketModelEvolver)->advanceStep ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::ICurveState^ Cephei::QL::Models::Marketmodels::CMarketModelEvolver::CurrentState::get ()
{
    try
    {
    	QuantLib::CurveState& _rv = (QuantLib::CurveState&)(*_ppMarketModelEvolver)->currentState ( );   
        Cephei::QL::Models::Marketmodels::CCurveState^ _nrv = REFNEW Cephei::QL::Models::Marketmodels::CCurveState (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::CMarketModelEvolver::CurrentStep::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMarketModelEvolver)->currentStep ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<UInt64>^ Cephei::QL::Models::Marketmodels::CMarketModelEvolver::Numeraires::get ()
{
    try
    {
    	std::vector<QuantLib::Size>& _rv = (std::vector<QuantLib::Size>&)(*_ppMarketModelEvolver)->numeraires ( );   
        Cephei::Core::IVector<UInt64>^ _nrv = REFNEW CoVector<UInt64> (REFNEW CUInt64Vector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::IMarketModelEvolver^ Cephei::QL::Models::Marketmodels::CMarketModelEvolver::SetInitialState (Cephei::QL::Models::Marketmodels::ICurveState^ prm1)
{
    CCurveState^ _Cprm1;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cprm1 = safe_cast<CCurveState^> (prm1);
        _Cprm1->Lock();
        QuantLib::CurveState& _prm1 = static_cast<QuantLib::CurveState&> (_Cprm1->GetReference ()); 
    	(*_ppMarketModelEvolver)->setInitialState ( _prm1 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
Double Cephei::QL::Models::Marketmodels::CMarketModelEvolver::StartNewPath::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppMarketModelEvolver)->startNewPath ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::CMarketModelEvolverVector::CMarketModelEvolverVector ()
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
}
Cephei::QL::Models::Marketmodels::CMarketModelEvolverVector::~CMarketModelEvolverVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
	_pVValue = NULL;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
	_pVBoost = NULL;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
	_pVHand = NULL;
#endif
}
Cephei::QL::Models::Marketmodels::CMarketModelEvolverVector::!CMarketModelEvolverVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
#endif
}
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::CMarketModelEvolverVector::CMarketModelEvolverVector (std::vector<QuantLib::MarketModelEvolver>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = &vec;
	_owner = owner;
}
Cephei::QL::Models::Marketmodels::CMarketModelEvolverVector::CMarketModelEvolverVector (std::vector<QuantLib::MarketModelEvolver> vec)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = new std::vector<QuantLib::MarketModelEvolver> (vec);
}
#endif
Cephei::QL::Models::Marketmodels::CMarketModelEvolverVector::CMarketModelEvolverVector (std::vector<boost::shared_ptr<QuantLib::MarketModelEvolver>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVBoost = &vec;
	_owner = owner;
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::CMarketModelEvolverVector::CMarketModelEvolverVector (std::vector<QuantLib::Handle<QuantLib::MarketModelEvolver>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVHand = &vec;
	_owner = owner;
}
#endif

int Cephei::QL::Models::Marketmodels::CMarketModelEvolverVector::Count::get ()
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
		return _pVValue->size();
	}
#endif    
	if (_pVBoost != NULL)
	{
		return _pVBoost->size ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		return _pVHand->size ();
	}
#endif
	return 0;		// should never happen
}
Cephei::QL::Models::Marketmodels::IMarketModelEvolver^ Cephei::QL::Models::Marketmodels::CMarketModelEvolverVector::default::get (int index) 
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
    	if ((size_t)index >= _pVValue->size()) throw REFNEW RangeException ();
		QuantLib::MarketModelEvolver& native = (*_pVValue)[index];
		return REFNEW CMarketModelEvolver (native, this);
	}
#endif    
	if (_pVBoost != NULL)
	{
    	if ((size_t)index >= _pVBoost->size()) throw REFNEW RangeException ();
		QuantLib::MarketModelEvolver& native = *(*_pVBoost)[index];
		return REFNEW CMarketModelEvolver (native, this);
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
    	if ((size_t)index >= _pVHand->size()) throw REFNEW RangeException ();
		QuantLib::MarketModelEvolver& native = *((*_pVHand)[index].currentLink ());
		return REFNEW CMarketModelEvolver (native, this);
	}
#endif
	return nullptr;		// can never be the case
}
void Cephei::QL::Models::Marketmodels::CMarketModelEvolverVector::default::set (int index, Cephei::QL::Models::Marketmodels::IMarketModelEvolver^ value) 
{
#ifdef STRUCT
	if (_pVValue != NULL)
	{
		if (_pVValue->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVValue->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVValue->push_back (dynamic_cast<CMarketModelEvolver^>(value)->GetReference ());
		}
        else
        {
            QuantLib::MarketModelEvolver& c = (*_pVValue)[index];
            c = dynamic_cast<CMarketModelEvolver^>(value)->GetReference ();
        }
	}
#endif   
	if (_pVBoost != NULL)
	{
		if (_pVBoost->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVBoost->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVBoost->push_back (dynamic_cast<CMarketModelEvolver^>(value)->GetShared ());
		}
		boost::shared_ptr<QuantLib::MarketModelEvolver>& c = (*_pVBoost)[index];
		c = dynamic_cast<CMarketModelEvolver^>(value)->GetShared ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		if (_pVHand->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVHand->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVHand->push_back (QuantLib::Handle<QuantLib::MarketModelEvolver> ());
		}
		QuantLib::Handle<QuantLib::MarketModelEvolver>& c = (*_pVHand)[index];
		boost::shared_ptr<QuantLib::MarketModelEvolver>& b = static_cast<boost::shared_ptr<QuantLib::MarketModelEvolver>>(c.currentLink());
		b = dynamic_cast<CMarketModelEvolver^>(value)->GetShared ();
	}
#endif
}
Cephei::Core::Generic::ICoCell<Cephei::QL::Models::Marketmodels::IMarketModelEvolver^>^ Cephei::QL::Models::Marketmodels::CMarketModelEvolverVector::CItem::get (int index) 
{
	return CellFactory::Instance->Create<IMarketModelEvolver^> (dynamic_cast<IMarketModelEvolver^>(default[index]));
}

void Cephei::QL::Models::Marketmodels::CMarketModelEvolverVector::CItem::set (int index, Cephei::Core::Generic::ICoCell<Cephei::QL::Models::Marketmodels::IMarketModelEvolver^>^ value) 
{
	default[index] = safe_cast<IMarketModelEvolver^>(value->Value);
}
#ifdef STRUCT
std::vector<QuantLib::MarketModelEvolver>& Cephei::QL::Models::Marketmodels::CMarketModelEvolverVector::GetReference ()
{
	 return *_pVValue;
}
#endif

std::vector<boost::shared_ptr<QuantLib::MarketModelEvolver>>& Cephei::QL::Models::Marketmodels::CMarketModelEvolverVector::GetShared ()
{
	return *_pVBoost;
}

#ifdef HANDLE
std::vector<QuantLib::Handle<QuantLib::MarketModelEvolver>>& Cephei::QL::Models::Marketmodels::CMarketModelEvolverVector::GetHandle ()
{
	return *_pVHand;
}
#endif
bool Cephei::QL::Models::Marketmodels::CMarketModelEvolverVector::PrepareFeature (NativeFeature feature)
{
	switch (feature)
	{
#ifdef STRUCT
		case NativeFeature::Value :
			if (_pVValue == NULL)
			{
				_pVValue = new std::vector<QuantLib::MarketModelEvolver> ();
				return true;
			}
			break;
#endif
        case NativeFeature::shared_ptr :
			if (_pVBoost == NULL)
			{
				_pVBoost = new std::vector<boost::shared_ptr<QuantLib::MarketModelEvolver>> ();
				return true;
			}
			break;
#ifdef HANDLE
        case NativeFeature::Handle :
            if (_pVHand == NULL)
            {
                _pVHand = new std::vector<QuantLib::Handle<QuantLib::MarketModelEvolver>> ();
                return true;
            }
            break;
#endif
	}
	return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

/* <summary>Create a single dimension array of MarketModelEvolver that can be passed into other function </summary> */
IVector<Cephei::QL::Models::Marketmodels::IMarketModelEvolver^>^ Cephei::QL::Models::Marketmodels::CMarketModelEvolver_Factory::CreateVector ()
 {
	 return REFNEW CoVector<IMarketModelEvolver^> (REFNEW CMarketModelEvolverVector ());
 }
IVector<Cephei::QL::Models::Marketmodels::IMarketModelEvolver^>^ Cephei::QL::Models::Marketmodels::CMarketModelEvolver_Factory::CreateVector (PLATFORM::Collections::Generic::IEnumerable<IMarketModelEvolver^>^ source)
 {
	 return REFNEW CoVector<IMarketModelEvolver^> (source, REFNEW CMarketModelEvolverVector ());
 }
/* <summary>Create a single dimension array of MarketModelEvolver with change events</summary> */
Cephei::Core::Generic::ICoCell<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Models::Marketmodels::IMarketModelEvolver^>^>^>^ Cephei::QL::Models::Marketmodels::CMarketModelEvolver_Factory::CreateCellVector ()
 {
	 return CellFactory::Instance->Create<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Models::Marketmodels::IMarketModelEvolver^>^>^> (REFNEW CoCellVector<Cephei::QL::Models::Marketmodels::IMarketModelEvolver^> (REFNEW CMarketModelEvolverVector ()));
 }
Cephei::Core::Generic::ICoCell<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Models::Marketmodels::IMarketModelEvolver^>^>^>^ Cephei::QL::Models::Marketmodels::CMarketModelEvolver_Factory::CreateCellVector (PLATFORM::Collections::Generic::IEnumerable<Cephei::Core::Generic::ICoCell<Cephei::QL::Models::Marketmodels::IMarketModelEvolver^>^>^ source)
 {
	 return CellFactory::Instance->Create<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Models::Marketmodels::IMarketModelEvolver^>^>^> (REFNEW CoCellVector<Cephei::QL::Models::Marketmodels::IMarketModelEvolver^> (source, REFNEW CMarketModelEvolverVector ()));
 }

